<?php
namespace  app\models;
require_once 'BaseModel.php';

class Lkeys extends BaseModel{
	/**
	 * jiahaomai
	 * 产生lkey保存在db
	 * @param unknown_type $user_id
	 * @param unknown_type $appid
	 */
	public static function genLkey($user_id, $appkey){
		$user_id = intval($user_id);
		$appkey = mysql_real_escape_string($appkey);
		$lkey = strtoupper(md5(time() . mt_rand(0, 10000) . substr('abcdefg', mt_rand(0, 7))));
		$expired_at = date('Y-m-d H:i:s', strtotime('+3 hours'));
		$now = date('Y-m-d H:i:s');
		$con = self::pdo_con(); 
		//先删除旧的
		$remove_sql = "delete from lkeys where user_id = {$user_id} and appkey = '{$appkey}'";
		$con->exec($remove_sql);
		$sql = "insert into lkeys values(0, {$user_id}, '{$appkey}', '{$lkey}', '{$expired_at}', '{$now}')";
		$con->exec($sql);
		return $lkey;
	}

	public static function saveLkey($arr){
		$user_id    = intval($arr['user_id']);
	    $appkey     = $arr['appkey'];
        $lkey       = $arr['lkey'];
		$expired_at = date('Y-m-d H:i:s', $arr['expired_at']);
        $created_at = date('Y-m-d H:i:s', $arr['ret_time']);

		$con = self::pdo_con();
		$sql = "insert into lkeys values(0, {$user_id}, '{$appkey}', '{$lkey}', '{$expired_at}', '{$created_at}')";
		$con->exec($sql);
	}
}
